package es.udc.fbellas.j2ee.tiendaweb.model.direccionpostal.DAO;

import java.sql.Connection;

import javax.sql.DataSource;

import es.udc.fbellas.j2ee.tiendaweb.model.direccionpostal.TO.DireccionPostalTO;
import es.udc.fbellas.j2ee.util.configuration.ConfigurationParametersManager;
import es.udc.fbellas.j2ee.util.exceptions.InternalErrorException;
import es.udc.fbellas.j2ee.util.sql.GeneralOperations;
import es.udc.fbellas.j2ee.util.sql.SimpleDataSource;

/**
 * A factory to get <code>SQLDireccionPostalDAO</code> objects.
 * <p>
 * Required configuration parameters:
 * <ul>
 * <li><code>SQLDireccionPostalDAOFactory/daoClassName</code>: it must specify the full
 * class name of the class implementing <code>SQLDireccionPostalDAO</code>.</li>
 * </ul>
 */
public class SQLDireccionPostalDAOFactory {

    private final static String DAO_CLASS_NAME_PARAMETER =
        "SQLDireccionPostalDAOFactory/daoClassName";

    private final static Class daoClass = getDAOClass();
    
    private SQLDireccionPostalDAOFactory() {}
    
    private static Class getDAOClass() {
    
        Class theClass = null;
    
        try {
        
            String daoClassName = 
                ConfigurationParametersManager.getParameter(
                    DAO_CLASS_NAME_PARAMETER);
            
            theClass = Class.forName(daoClassName);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return theClass;
        
    }
    
    public static SQLDireccionPostalDAO getDAO() throws InternalErrorException {
        
        try {        
            return (SQLDireccionPostalDAO) daoClass.newInstance();
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }
        
    }
    
    /* 
     * Test code. Uncomment for testing. Test this class and any class
     * implementing "SQLDireccionPostalDAO".
     */         
    public static void main (String[] args) {    
        
        Connection connection = null;
    
        try {        
            
            /* Get a connection. */
            DataSource dataSource = new SimpleDataSource();
            connection = dataSource.getConnection();        
                    
            /* Get dao. */
            SQLDireccionPostalDAO dao = SQLDireccionPostalDAOFactory.getDAO();

            /* Test "SQLTarjetaBAncariaDAO::create". */
            int idDir =14325;
            System.out.println("Test for 'SQLDireccionPostalDAO::create'");
            DireccionPostalTO direccionPostalTO1 = new DireccionPostalTO(0,"PEPE","JUAN FLOREZ",37,"A",15011);
            direccionPostalTO1 = dao.create(connection, direccionPostalTO1);
            System.out.println("direccionPostalTO1 created => " + direccionPostalTO1);
            
            /* Test "SQLDireccionPostalDAO::exists". */
            System.out.println("Test for 'SQLDireccionPostalDAO::exists'");
            if (!dao.exists(connection, direccionPostalTO1.getIdDir())) {
                throw new Exception("Can not find direccion postal with " +
                    "id direccion = " + 
                    direccionPostalTO1.getIdDir());
            }
            
            /* Test "SQLDireccionPostalDAO::find". */
            System.out.println("Test for 'SQLDireccionPostalDAO::find'");
            System.out.println(dao.find(connection,
                direccionPostalTO1.getIdDir()));
            
            /* Test "SQLDireccionPostalDAO::update". */
            System.out.println("Test for 'SQLDireccionPostalDAO::update'");
            direccionPostalTO1.setNombreReceptor("JUANITA");
            dao.update(connection, direccionPostalTO1);
            System.out.println(dao.find(connection,
                direccionPostalTO1.getIdDir()));

            /* Tests OK. */
            System.out.println("Tests OK !!!!");
        
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                GeneralOperations.closeConnection(connection);
            } catch (InternalErrorException e) {
                e.printStackTrace();
            }
        }
                
    }    
    
}
